
rem
bbdoc: Game2D Engine.
endrem
Type TEngine

	Field _timer:TFixedTime
	Field _graphics:TGraphics	
	Field _game:TGame
	
	'bools
	Field _paused:Int
	Field _running:Int	
	Field _vsync:Int
	
	'key code for pause key. default is 'p'
	Field _pauseKey:Int = KEY_P


	Method New()
		_timer = New TFixedTime
	End Method
	
	
	rem
	bbdoc: Default Constructor.
	endrem
	Function Create:TEngine(game:TGame)
		Local e:TEngine = New TEngine
		e.SetGame(game)
		Return e
	End Function
	
	
	
	rem
	bbdoc: Starts the engine.
	about: Calls game Startup() method first.
	endrem
	Method Start()
		_game.Startup()
		_running = True
		_paused = False
		_vsync = True
		_timer.Reset()
		run()
	End Method

	
	Method run()
	
		' make sure that all systems have had 1 update ( have run Added() method )
		_game.Update(1)
		
		While _running
			If Not _paused
				_timer.Update()
				While _timer.TimeStepNeeded()
					_game.Update(_timer.GetDeltaTime())
				Wend
			End If
			
			If KeyHit(_pauseKey)
				SetPaused(Not _paused)
				If _paused = False Then _timer.Reset()
			EndIf
			
			Cls
			_game.Render(_timer.GetTweening())
			Flip(_vsync)
			
			If AppTerminate() Then _running = False
		Wend
		Stop()
	End Method	
		
	
	Method Stop()	
		_game.CleanUp()
	
		'save config etc here?		
	
		_game = Null	
		_timer = Null
		EndGraphics()
	End Method
	
	
	rem
	bbdoc: Sets vertical blank flag.
	endrem
	Method SetVSync(bool:Int = True)
		_vsync = bool
	End Method
		
	
	rem
	bbdoc: Returns vertical blank flag.
	endrem
	Method GetVSync:Int()
		Return _vsync
	End Method
	
		
	rem
	bbdoc: Sets engine pause mode.
	endrem
	Method SetPaused(bool:Int)
		_paused = bool
	End Method
		
	
	rem
	bbdoc: Returns true when the engine is paused.
	endrem
	Method IsPaused:Int()
		Return _paused
	End Method
	
	
	rem
	bbdoc: Changes the default engine pause key.
	endrem
	Method SetPauseKey(keycode:Int)
		_pauseKey = keycode
	End Method
	
		
	Method SetGame(g:TGame)
		_game = g
        _game.SetEngine(Self)
	End Method
		
	
	rem
	bbdoc: Returns the current game.
	endrem
	Method GetGame:TGame()
		Return _game
	End Method
	
		
	rem
	bbdoc: Stops the engine on the next update.
	endrem
	Method RequestStop()
		_running = False
	End Method
	
		
	rem
	bbdoc: Sets engine update frequency.
	endrem
	Method SetUpdateFrequency(fr:Int)
		_timer.SetUpdateFrequency(fr)
	End Method
		
	
	Rem
	bbdoc: Sets physical screen resolution and graphics driver.
	about: A depth of 0 means a Windowed game.
	endrem
	Method SetDisplayMode(w:Int, h:Int, depth:Int = 0, herz:Int = 60, openGL:Int = False)
		
		If openGL
			SetGraphicsDriver(GLMax2DDriver())
		Else
			?Win32
			SetGraphicsDriver(D3D9Max2DDriver())
			?
		End If
	
		If Not GraphicsModeExists(w, h, depth, herz)
			If GraphicsModeExists(800,600,0,0)
				w = 800
				h = 600
				depth = 0
				herz = 0
				openGL = False
			Else
				RuntimeError("Could not create graphics driver!")	
			End If
		End If
		_graphics = Graphics(w, h, depth, herz)
				
	End Method
	
End Type
